//
//  HRZ_ShoppingCartHeaderView.m
//  EMarket
//
//  Created by 石头 on 2018/5/13.
//  Copyright © 2018年 好日子. All rights reserved.
//

#import "HRZShoppingCartHeaderView.h"

#import "HRZStoreModel.h"
#import "HRZGoodsModel.h"

@interface HRZShoppingCartHeaderView()

@property (strong, nonatomic) UIButton *clickBtn;
@property (strong, nonatomic) UIButton *editBtn;
@property (nonatomic, strong) UIButton *storeBtn;
@property (strong, nonatomic) UILabel *storeNameLabel;
@property (strong, nonatomic) UIImageView *storeImageView;

@property (strong, nonatomic) NSLayoutConstraint *selectBtnWidth;

@end

@implementation HRZShoppingCartHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self){
        [self addSubview:self.clickBtn];
        [self addSubview:self.storeImageView];
        [self addSubview:self.storeNameLabel];
        [self addSubview:self.editBtn];
        [self addSubview:self.storeBtn];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.clickBtn];
        [self addSubview:self.storeImageView];
        [self addSubview:self.storeNameLabel];
        [self addSubview:self.editBtn];
        [self addSubview:self.storeBtn];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    
    [self.clickBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self).mas_offset(10);
        make.bottom.mas_equalTo(self);
        make.width.mas_equalTo(30);
    }];
    
    [self.storeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.left.mas_equalTo(self.clickBtn.mas_right).mas_offset(10);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    [self.storeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self);
        make.left.mas_equalTo(self.clickBtn.mas_right).mas_offset(40);
    }];
    
    [self.editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.right.mas_equalTo(self).mas_offset(-10);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
    }];
    
    [self.storeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self.clickBtn.mas_right);
        make.width.mas_equalTo(self);
        make.height.mas_equalTo(self);
    }];
}

#pragma mark - ----  eventResponce  ----

- (void)click:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        [sender setImage:[UIImage imageNamed:@"clicked"] forState:(UIControlStateNormal)];
    } else {
        [sender setImage:[UIImage imageNamed:@"unClick"] forState:(UIControlStateNormal)];
    }
    if (self.clickBtn) {
        self.ClickBlock(sender.selected);
    }
}
- (void)onEditButtonClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        [sender setTitle:@"完成" forState:UIControlStateNormal];
        [sender setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    } else {
        [sender setTitle:@"编辑" forState:UIControlStateNormal];
        [sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    if (self.EditBlock) {
        self.EditBlock(sender.selected);
    }
}

- (void)onStoreBtnClick{
    if (self.StoreBlock) {
        self.StoreBlock(_storeModel);
    }
    
}

- (void)updateCurrentConstraints {
    [super updateConstraints];
    
    self.selectBtnWidth.constant = -10;
    self.editBtn.hidden = YES;
}
#pragma mark - ----  Setter  ----
- (void)setStoreModel:(HRZStoreModel *)storeModel {
    self.storeNameLabel.text = storeModel.storeName;
    self.isClick = storeModel.isSelect;
    self.isEdit = storeModel.isEdit;
}

- (void)setIsClick:(BOOL)isClick {
    _isClick = isClick;
    self.clickBtn.selected = isClick;
    if (isClick) {
        [self.clickBtn setImage:[UIImage imageNamed:@"clicked"] forState:(UIControlStateNormal)];
    } else {
        [self.clickBtn setImage:[UIImage imageNamed:@"unClick"] forState:(UIControlStateNormal)];
    }
}

- (void)setIsEdit:(BOOL)isEdit {
    _isEdit = isEdit;
    self.editBtn.selected = isEdit;
    if (isEdit) {
        [self.editBtn setTitle:@"完成" forState:UIControlStateNormal];
        [self.editBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    } else {
        [self.editBtn setTitle:@"编辑" forState:UIControlStateNormal];
        [self.editBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
}

#pragma mark - ----  Getter  ----
- (UIButton *)clickBtn{
    if (!_clickBtn) {
        _clickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_clickBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _clickBtn;
}

- (UIImageView *)storeImageView{
    if (!_storeImageView) {
        _storeImageView = [[UIImageView alloc] init];
        _storeImageView.contentMode = UIViewContentModeScaleAspectFit;
        _storeImageView.frame = CGRectMake(0, 0, 20, 20);
        _storeImageView.image = [UIImage imageNamed:@"StoreName"];
    }
    return _storeImageView;
}

- (UILabel *)storeNameLabel{
    if (!_storeNameLabel) {
        _storeNameLabel = [[UILabel alloc] init];
        _storeNameLabel.font = PFR15Font;
    }
    return _storeNameLabel;
}

- (UIButton *)editBtn{
    if (!_editBtn) {
        _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _editBtn.hidden = YES;
    }
    return _editBtn;
}

- (UIButton *)storeBtn{
    if (!_storeBtn) {
        _storeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_storeBtn addTarget:self action:@selector(onStoreBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _storeBtn;
}


@end
